﻿using Guest_room.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Guest_room.admin
{
    public partial class uproom : Form
    {
        public uproom()
        {
            InitializeComponent();
        }
        Guest_roomDataContext db = new Guest_roomDataContext();
        protected void bind()//加载客房信息
        {
            var room = from r in db.客房基本信息表
                          select new
                          {
                            r.客房编号,
                            r.档次编号,
                            r.客房单价,
                            r.客房状态,
                            r.客房电话,
                            r.客房图片
                          };
            dataGridView1.DataSource = room;
            //列宽铺满容器
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            //数据集倒序
            dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
        }
        void grade()//加载档次名称
        {
            var grade = from r in db.客房档次表
                        select r.客房档次;
            combo_department.DataSource = grade;
        }
        void get_maxid()//获取最大编号
        {
            var maxid = (from r in db.客房基本信息表
                         select r.客房编号).Max();
            string num = maxid.Substring(2, 3);
            int id = Convert.ToInt32(num);
            id++;
            string newid = id.ToString();
            while (newid.Length < 3)
            {
                newid = "0" + newid;
            }
            string roomid = "KF" + newid;
            textBox5.Text = roomid;
            //获取档次编号
            textBox1.Text = (from r in db.客房档次表
                             where r.客房档次 == combo_department.Text
                             select r.档次编号).FirstOrDefault();
            
        }
        private void uproom_Load(object sender, EventArgs e)//窗体加载函数
        {
            grade();
            get_maxid();
            textBox5.Enabled = false;
            textBox1.Enabled = false;
            textBox6.Enabled = false;
            textBox8.Enabled = false;
        }

        GuestRoomService guestRoomService = new GuestRoomService();
        private void combo_department_SelectedIndexChanged(object sender, EventArgs e)//按照档次编号查询
        {
            string roomGrade = combo_department.Text;

            // 获取客房信息
            var rooms = guestRoomService.GetRoomsByGrade(roomGrade);

            if (tabControl1.SelectedTab == tabPage1)
            {
                dataGridView1.DataSource = rooms;
                // 数据集倒序
                //dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
                // 列宽铺满容器
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }

            // 获取档次编号
            string gradeId = guestRoomService.GetGradeIdByGrade(roomGrade);
            textBox1.Text = gradeId;
            textBox6.Text = gradeId;

            label4.Text = "共有" + dataGridView1.RowCount + "条记录";
        }

        private void button1_Click(object sender, EventArgs e)//显示全部
        {
            bind();
            label4.Text = "共有" + dataGridView1.RowCount + "条记录";
        }
        private string savedImagePath,modifyImagePath; // 保存图片的路径
        private void pictureBox1_Click(object sender, EventArgs e)//添加图片
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "图片文件(*.jpg,*.png,*.gif)|*.jpg;*.png;*.gif";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(open.FileName);
                // textBox4.Text = open.FileName;
                //随机3位数
                Random rd = new Random();
                int num = rd.Next(100, 999);
                textBox2.Text = num.ToString();
                // 随机电话000-00000000
                Random rd1 = new Random();
                int num1 = rd1.Next(10000000, 99999990);
                textBox3.Text = "010-"+ num1.ToString();

            }
            //图片自适应容器
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            savedImagePath = open.FileName;

        }

        private void button2_Click(object sender, EventArgs e)//添加
        {
            if (textBox5.Text == "" || textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || savedImagePath == "")
            {
                MessageBox.Show("请填写完整信息");
            }
            else
            {
                try
                {
                    //添加图片
                    string path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, 
                        Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));      
                    string path1 = path + "\\bin\\Debug\\Prod_Images\\room\\" + textBox5.Text + ".jpg";
                    System.IO.File.Copy(savedImagePath, path1, true);   
                    //添加客房信息
                    客房基本信息表 room = new 客房基本信息表();
                    room.客房编号 = textBox5.Text;
                    room.档次编号 = textBox1.Text;
                    room.客房单价 = int.Parse(textBox2.Text);
                    room.客房状态 = "空闲";
                    room.客房电话 = textBox3.Text;
                    room.客房图片 = textBox5.Text + ".jpg";
                    db.客房基本信息表.InsertOnSubmit(room);
                    // 更新客房档次表的空房数
                    客房档次表 grade1 = db.客房档次表.Single(r => r.档次编号 == textBox1.Text);
                    grade1.空房数 = grade1.空房数 + 1;
                    db.SubmitChanges();
                    MessageBox.Show("添加成功");
                    // bind();
                    combo_department_SelectedIndexChanged(sender, e);
                    get_maxid();
                    pictureBox1.Image = null;
                    textBox2.Text = "";
                    textBox3.Text = "";

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //添加日志文件
                日志表 log = new 日志表();
                log.操作员编号 = Login.userid??Home.userid;
                log.操作员 = Login.username??Home.username;
                log.操作时间 = DateTime.Now;
                log.操作类型 = "添加";
                log.操作内容 = "添加了客房编号为" + textBox5.Text + "的客房信息";
                log.操作表 = "客房基本信息表";

                db.日志表.InsertOnSubmit(log);
                db.SubmitChanges();

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//点击后复制到文本框
        {
            //跳转到修改界面
            tabControl1.SelectedTab = tabPage2;
            //双击后获取行索引
            int index = dataGridView1.CurrentRow.Index;
            string roomid = dataGridView1.Rows[index].Cells[0].Value.ToString();
            if(dataGridView1.Rows.Count>0)
            {
                textBox8.Text = dataGridView1.Rows[index].Cells["客房编号"].Value.ToString();
                textBox6.Text = dataGridView1.Rows[index].Cells["档次编号"].Value.ToString();
                textBox7.Text = dataGridView1.Rows[index].Cells["客房单价"].Value.ToString();
                textBox4.Text = dataGridView1.Rows[index].Cells["客房电话"].Value.ToString();
                try{
                    pictureBox2.Image = Image.FromFile(Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
                            Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"))
                            + "\\bin\\Debug\\Prod_Images\\room\\" + dataGridView1.Rows[index].Cells["客房图片"].Value.ToString());
                }
                catch
                {
                    pictureBox2.Image = null;
                }
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            }
        }

        private void button3_Click(object sender, EventArgs e)//修改
        {
            if (textBox8.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("请填写完整信息");
            }
            else
            {
                try
                {
                    if(modifyImagePath!=null)
                    {
                        string path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0,
                        Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
                        string path1 = path + "\\bin\\Debug\\Prod_Images\\room\\" + textBox5.Text + ".jpg";
                        System.IO.File.Copy(modifyImagePath, path1, true);
                    }
                    
                    //修改客房信息
                    客房基本信息表 room = db.客房基本信息表.Single(r => r.客房编号 == textBox8.Text);
                    room.档次编号 = textBox6.Text;
                    room.客房单价 = Convert.ToInt32(textBox7.Text);
                    room.客房电话 = textBox4.Text;
                    db.SubmitChanges();
                    MessageBox.Show("修改成功");
                    bind();
                    combo_department_SelectedIndexChanged(sender, e);
                    get_maxid();
                    pictureBox2.Image = null;
                    textBox7.Text = "";
                    textBox4.Text = "";

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //添加日志文件
                日志表 log = new 日志表();
                log.操作员编号 = Login.userid ?? Home.userid;
                log.操作员 = Login.username ?? Home.username;
                log.操作时间 = DateTime.Now;
                log.操作类型 = "修改";
                log.操作内容 = "修改了客房编号为" + textBox8.Text + "的客房信息";
                log.操作表 = "客房基本信息表";

                db.日志表.InsertOnSubmit(log);
                db.SubmitChanges();
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)//修改图片
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "图片文件(*.jpg,*.png,*.gif)|*.jpg;*.png;*.gif";
            if (open.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(open.FileName);
                // textBox4.Text = open.FileName;
            }
            //图片自适应容器
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            modifyImagePath = open.FileName;
        }
    }
}
